﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Project.Core.Config.Common.Lang;
using Project.Core.Test.Entity;

namespace Project.Core.Test.Controllers {
    /// <summary>
    /// 书接口
    /// </summary>
    public class SysBookController : TestController<SysBookController> {
        /// <summary>
        /// 获取书信息
        /// </summary>
        [HttpGet]
        [Route("/book/getData")]
        public Result GetData() {
            var list = SysBookService.GetAll();
            return Result.Succ(list);
        }
        /// <summary>
        /// 分页获取书信息
        /// </summary>
        [HttpGet]
        [Route("/book/getDataPage")]
        public Result GetDataPage()
        {
            var list = SysBookService.Page(GetPage());
            return Result.Succ(list);
        }
        /// <summary>
        /// 获取书信息
        /// </summary>
        [HttpGet]
        [Route("/book/getDataAsync")]
        public async Task<Result> GetDataAsync() {
            var list = await SysBookService.GetAllAsync();
            return Result.Succ(list);
        }

        /// <summary>
        /// 添加书信息
        /// </summary>
        [HttpPost]
        [Route("/book/addOrUpdateData")]
        [Authorize(Roles = "book:add")]
        public Result AddOrUpdateData(SysBook input) {
            if (input.Id == 0) {
                SysBookService.Add(input);
                return Result.Succ("添加成功");
            } else {
                var data = SysBookService.GetById(input.Id);
                if (data == null) {
                    return Result.Fail("更新失败");
                } else {
                    data.Name = input.Name;
                    data.Description = input.Description;
                    data.Price = input.Price;
                    data.Category = input.Category;
                    SysBookService.Update(data);
                    return Result.Succ("更新成功");
                }
            }
        }
        /// <summary>
        /// 添加或更新书信息
        /// </summary>
        [HttpPost]
        [Route("/book/addOrUpdateDataAsync")]
        [Authorize(Roles = "book:add")]
        public async Task<Result> AddOrUpdateDataAsync(SysBook input) {
            if (input.Id == 0) {
                await SysBookService.AddAsync(input);
                return Result.Succ("添加成功");
            } else {
                var data = await SysBookService.GetByIdAsync(input.Id);
                if (data == null) {
                    return Result.Fail("更新失败");
                } else {
                    data.Name = input.Name;
                    data.Description = input.Description;
                    data.Price = input.Price;
                    data.Category = input.Category;
                    await SysBookService.UpdateAsync(data);
                    return Result.Succ("更新成功");
                }
            }
        }

        /// <summary>
        /// 删除书信息
        /// </summary>
        [HttpGet]
        [Route("/book/delData")]
        [Authorize(Roles = "book:delete")]
        public Result DelData(int bookId) {
            SysBookService.DeleteById(bookId);
            return Result.Succ("删除成功");
        }

        /// <summary>
        /// 更新书信息
        /// </summary>
        [HttpPost]
        [Route("/book/updateData")]
        [Authorize(Roles = "book:update")]
        [ApiExplorerSettings(IgnoreApi = true)]
        public Result UpdateData(SysBook book) {
            var data = SysBookService.GetById(book.Id);
            if (data == null) {
                return Result.Fail("更新失败");
            } else {
                data.Name = book.Name;
                data.Description = book.Description;
                data.Price = book.Price;
                data.Category = book.Category;
                SysBookService.Update(data);
                return Result.Succ("更新成功");
            }
        }
    }
}
